<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.ico">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.css" integrity="sha256-6cQIC71/iBIYXFK+0RHAvwmjwWzkWd+r7v/BX3/vZDc=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/themes/green/pace-theme-minimal.css">
  <script src="https://cdnjs.cloudflare.com/ajax/libs/pace/1.2.4/pace.min.js" integrity="sha256-gqd7YTjg/BtfqWSwsJOvndl0Bxc8gFImLEkXQT8+qj0=" crossorigin="anonymous"></script>

<script class="next-config" data-name="main" type="application/json">{"hostname":"sumumm.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":true,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文主要是GitHub的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:type" content="article">
<meta property="og:title" content="LV01-Git-05-远程仓库-02-远程分支">
<meta property="og:url" content="https://sumumm.github.io/post/fb1288db.html">
<meta property="og:site_name" content="苏木">
<meta property="og:description" content="本文主要是GitHub的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/remote-branches-1.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/remote-branches-2.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/remote-branches-3.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/remote-branches-4.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230624215053917.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230624215338926.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230624215703682.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230624215823917.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230624220607035.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230624222045692.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230625063859546.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230624222444548.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230624222606888.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230625070958747.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230625071210402.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20241025072136813.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20241025072458099.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/v2-dd3f82b802cf5f09a024a7c6f0ffe694_b.jpg">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/v2-e45c2455917162e1095eb497dd08766a_b.jpg">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230625193740801.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230625193202012.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230625193310502.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230625200311770.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230625200752434.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230625201323376.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230625205412224.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230625211216587.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230625211355932.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230625211603428.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230625211742766.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230625212229039.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230625212418890.png">
<meta property="og:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230625213048886.png">
<meta property="article:published_time" content="2023-07-02T07:14:34.000Z">
<meta property="article:modified_time" content="2025-06-13T16:25:57.070Z">
<meta property="article:author" content="苏木">
<meta property="article:tag" content="Git">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/remote-branches-1.png">


<link rel="canonical" href="https://sumumm.github.io/post/fb1288db.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://sumumm.github.io/post/fb1288db.html","path":"post/fb1288db.html","title":"LV01-Git-05-远程仓库-02-远程分支"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>LV01-Git-05-远程仓库-02-远程分支 | 苏木</title>
  








    <script src="/js/browser_tools_disable.js"></script>

  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<!-- hexo injector head_end start --><link rel="stylesheet" href="https://unpkg.com/hexo-next-tags-plus@latest/lib/tag_plus.css" media="defer" onload="this.media='all'"><!-- hexo injector head_end end --></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">苏木</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">我的学习之路</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>苏木的家</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类页<span class="badge">42</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档页<span class="badge">673</span></a></li><li class="menu-item menu-item-flink"><a href="/flink/" rel="section"><i class="fa fa-link fa-fw"></i>友人帐</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%80%E3%80%81%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF"><span class="nav-text">一、远程分支</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF%E8%AF%B4%E6%98%8E"><span class="nav-text">1. 远程分支说明</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF%E5%AE%9E%E4%BE%8B"><span class="nav-text">2. 远程分支实例</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%8C%E3%80%81%E5%88%9B%E5%BB%BA%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93"><span class="nav-text">二、创建远程仓库</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E7%A9%BA%E7%9A%84%E4%BB%93%E5%BA%93"><span class="nav-text">1. 空的仓库</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-%E4%BB%93%E5%BA%93%E8%AF%B4%E6%98%8E"><span class="nav-text">1.1 仓库说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-%E5%88%9B%E5%BB%BA%E7%A9%BA%E4%BB%93%E5%BA%93"><span class="nav-text">1.2 创建空仓库</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E9%9D%9E%E7%A9%BA%E4%BB%93%E5%BA%93"><span class="nav-text">2. 非空仓库</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-%E4%BB%93%E5%BA%93%E8%AF%B4%E6%98%8E"><span class="nav-text">2.1 仓库说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-%E5%88%9B%E5%BB%BA%E9%9D%9E%E7%A9%BA%E4%BB%93%E5%BA%93"><span class="nav-text">2.2 创建非空仓库</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E6%B5%8B%E8%AF%95%E8%AF%B4%E6%98%8E"><span class="nav-text">3. 测试说明</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%89%E3%80%81%E5%85%8B%E9%9A%86%E4%BB%93%E5%BA%93"><span class="nav-text">三、克隆仓库</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-git-clone%E5%91%BD%E4%BB%A4%E8%AF%A6%E7%BB%86%E8%AF%B4%E6%98%8E"><span class="nav-text">1. git clone命令详细说明</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%9F%BA%E6%9C%ACclone%E5%91%BD%E4%BB%A4"><span class="nav-text">2. 基本clone命令</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-%E5%91%BD%E4%BB%A4%E8%AF%B4%E6%98%8E"><span class="nav-text">2.1 命令说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">2.2 使用实例</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-1-HTTPS%E5%9C%B0%E5%9D%80"><span class="nav-text">2.2.1 HTTPS地址</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-2-SSH%E5%9C%B0%E5%9D%80"><span class="nav-text">2.2.2 SSH地址</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E5%85%8B%E9%9A%86%E6%8C%87%E5%AE%9A%E7%89%88%E6%9C%AC"><span class="nav-text">3. 克隆指定版本</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E5%85%8B%E9%9A%86%E6%8C%87%E5%AE%9A%E5%88%86%E6%94%AF"><span class="nav-text">4. 克隆指定分支</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-1-%E5%91%BD%E4%BB%A4%E8%AF%B4%E6%98%8E"><span class="nav-text">4.1 命令说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">4.2 使用实例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-depth%E5%8F%82%E6%95%B0"><span class="nav-text">5. depth参数</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%9B%E3%80%81%E6%9C%AC%E5%9C%B0%E4%B8%8E%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93%E7%9A%84%E8%81%94%E7%B3%BB%EF%BC%9F"><span class="nav-text">四、本地与远程仓库的联系？</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E6%A6%82%E8%BF%B0"><span class="nav-text">1. 概述</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-%E6%83%85%E5%86%B5%E8%AF%B4%E6%98%8E"><span class="nav-text">1.1 情况说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-%E5%91%BD%E4%BB%A4%E6%B1%87%E6%80%BB"><span class="nav-text">1.2 命令汇总</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93%E5%87%86%E5%A4%87"><span class="nav-text">2. 本地仓库准备</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-%E4%BB%93%E5%BA%93%E8%AF%B4%E6%98%8E-1"><span class="nav-text">2.1 仓库说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93%E5%88%9B%E5%BB%BA"><span class="nav-text">2.2 本地仓库创建</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-3-%E4%B8%A4%E4%B8%AA%E5%88%86%E6%94%AF%E6%83%85%E5%86%B5"><span class="nav-text">2.3 两个分支情况</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-3-1-master%E5%88%86%E6%94%AF"><span class="nav-text">2.3.1 master分支</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-3-2-dev%E5%88%86%E6%94%AF"><span class="nav-text">2.3.2 dev分支</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E6%B7%BB%E5%8A%A0%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93%E4%B8%8E%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93%E8%BF%9E%E6%8E%A5"><span class="nav-text">3. 添加本地仓库与远程仓库连接</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-%E5%91%BD%E4%BB%A4%E8%AF%B4%E6%98%8E"><span class="nav-text">3.1 命令说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">3.2 使用实例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E6%9F%A5%E7%9C%8B%E8%BF%9C%E7%A8%8B%E5%BC%95%E7%94%A8%E7%9A%84%E4%BF%A1%E6%81%AF"><span class="nav-text">4. 查看远程引用的信息</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-1-%E5%91%BD%E4%BB%A4%E8%AF%B4%E6%98%8E-1"><span class="nav-text">4.1 命令说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B-1"><span class="nav-text">4.2 使用实例</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-3-%E5%A4%9A%E4%B8%AAremote%E5%90%8D%EF%BC%9F"><span class="nav-text">4.3 多个remote名？</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-%E7%A7%BB%E9%99%A4%E4%B8%8E%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93%E7%9A%84%E8%BF%9E%E6%8E%A5"><span class="nav-text">5. 移除与远程仓库的连接</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#5-1-%E5%91%BD%E4%BB%A4%E8%AF%B4%E6%98%8E"><span class="nav-text">5.1 命令说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B"><span class="nav-text">5.2 使用实例</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BA%94%E3%80%81%E6%8E%A8%E9%80%81%E5%88%B0%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93"><span class="nav-text">五、推送到远程仓库</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E6%A6%82%E8%BF%B0-1"><span class="nav-text">1. 概述</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E6%8E%A8%E9%80%81%E5%88%B0%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93"><span class="nav-text">2. 推送到远程仓库</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-%E5%91%BD%E4%BB%A4%E8%AF%B4%E6%98%8E-1"><span class="nav-text">2.1 命令说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B-1"><span class="nav-text">2.2 使用实例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E4%B8%80%E4%B8%AA%E9%97%AE%E9%A2%98"><span class="nav-text">3. 一个问题</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#4-%E5%BC%BA%E5%88%B6%E6%8E%A8%E9%80%81"><span class="nav-text">4. 强制推送</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#4-1-%E5%91%BD%E4%BB%A4%E8%AF%B4%E6%98%8E-2"><span class="nav-text">4.1 命令说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B-2"><span class="nav-text">4.2 使用实例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#5-%E5%88%A0%E9%99%A4%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF"><span class="nav-text">5. 删除远程分支</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#5-1-%E5%91%BD%E4%BB%A4%E8%AF%B4%E6%98%8E-1"><span class="nav-text">5.1 命令说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#5-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B-1"><span class="nav-text">5.2 使用实例</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%85%AD%E3%80%81%E4%BB%8E%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93%E6%8B%89%E5%8F%96%E4%BB%A3%E7%A0%81"><span class="nav-text">六、从远程仓库拉取代码</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E6%A6%82%E8%BF%B0-2"><span class="nav-text">1. 概述</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1-1-%E5%91%BD%E4%BB%A4%E6%B1%87%E6%80%BB"><span class="nav-text">1.1 命令汇总</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-2-%E4%BB%93%E5%BA%93%E5%87%86%E5%A4%87"><span class="nav-text">1.2 仓库准备</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E6%9B%B4%E6%96%B0%E6%9C%AC%E5%9C%B0%E4%BB%93%E5%BA%93"><span class="nav-text">2. 更新本地仓库</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-%E5%91%BD%E4%BB%A4%E8%AF%B4%E6%98%8E-2"><span class="nav-text">2.1 命令说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B-2"><span class="nav-text">2.2 使用实例</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%83%E3%80%81%E8%B7%9F%E8%B8%AA%E5%88%86%E6%94%AF"><span class="nav-text">七、跟踪分支</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-%E6%A6%82%E8%BF%B0-3"><span class="nav-text">1. 概述</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-%E5%88%87%E6%8D%A2%E5%88%86%E6%94%AF"><span class="nav-text">2. 切换分支</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#2-1-%E5%91%BD%E4%BB%A4%E8%AF%B4%E6%98%8E-3"><span class="nav-text">2.1 命令说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B-3"><span class="nav-text">2.2 使用实例</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-%E6%9F%A5%E7%9C%8B%E8%AE%BE%E7%BD%AE%E7%9A%84%E8%B7%9F%E8%B8%AA%E5%88%86%E6%94%AF"><span class="nav-text">3. 查看设置的跟踪分支</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-1-%E5%91%BD%E4%BB%A4%E8%AF%B4%E6%98%8E-1"><span class="nav-text">3.1 命令说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-2-%E4%BD%BF%E7%94%A8%E5%AE%9E%E4%BE%8B-1"><span class="nav-text">3.2 使用实例</span></a></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="苏木"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">苏木</p>
  <div class="site-description" itemprop="description">莫道桑榆晚，为霞尚满天</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">673</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">42</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">43</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/sumumm" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;sumumm" rel="noopener me" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://sumumm.github.io/post/fb1288db.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="苏木">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏木">
      <meta itemprop="description" content="莫道桑榆晚，为霞尚满天">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="LV01-Git-05-远程仓库-02-远程分支 | 苏木">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          LV01-Git-05-远程仓库-02-远程分支
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-07-02 15:14:34" itemprop="dateCreated datePublished" datetime="2023-07-02T15:14:34+08:00">2023-07-02</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/" itemprop="url" rel="index"><span itemprop="name">开发工具</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/" itemprop="url" rel="index"><span itemprop="name">03版本控制</span></a>
        </span>
    </span>

  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>9.9k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>36 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文主要是GitHub的相关笔记，若笔记中有错误或者不合适的地方，欢迎批评指正😃。</p>
<span id="more"></span>

<!-- Photo: https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/ -->

<details class="folding-tag" blue><summary> 点击查看使用工具及版本 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center" width=150px>Windows</td>        <td align="left">windows11</td>    </tr>    <tr>        <td align="center">Ubuntu</td>        <td align="left">Ubuntu16.04的64位版本</td>      </tr>    <tr>        <td align="center">VMware® Workstation 16 Pro</td>        <td align="left">16.2.3 build-19376536</td>      </tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看本文参考资料 </summary>
              <div class='content'>
              <table>    <tr><td align="center">参考方向  </td><td align="center">参考原文</td></tr>    <tr><td align="left">Git 官网</td><td align="left"><a href="https://git-scm.com/" target="_blank">https://git-scm.com/ <i class="fa fa-external-link-alt"></i> </a></td></tr>    <tr><td align="left">Git 官方文档</td><td align="left"><a href="https://git-scm.com/doc" target="_blank">https://git-scm.com/doc <i class="fa fa-external-link-alt"></i> </a></td></tr>    <tr><td align="left">Pro Git Book</td><td align="left"><a href="https://git-scm.com/book/zh/v2" target="_blank">https://git-scm.com/book/zh/v2 <i class="fa fa-external-link-alt"></i> </a></td></tr>    <tr><td align="left">Git 快速使用指南</td><td align="left"><a href="https://training.github.com/downloads/zh_CN/github-git-cheat-sheet/" target="_blank">https://training.github.com/downloads/zh_CN/github-git-cheat-sheet/ <i class="fa fa-external-link-alt"></i> </a></td></tr>    <tr><td align="left">Visual Git Cheat Sheet</td><td align="left"><a href="https://ndpsoftware.com/git-cheatsheet.html#loc=index" target="_blank">https://ndpsoftware.com/git-cheatsheet.html#loc=index <i class="fa fa-external-link-alt"></i> </a></td></tr></table>
              </div>
            </details>

<details class="folding-tag" blue><summary> 点击查看相关文件下载 </summary>
              <div class='content'>
              <table>    <tr>        <td align="center">---</td>        <td align="left">--- <a href="" target="_blank">  <i class="fa fa-external-link-alt"></i></a></td>      </tr></table>
              </div>
            </details>

<p>我们上一节简单了解可一下GitHub以及一些相关命令，我们这部分就来深入了解一下这些命令怎么用。</p>
<h1 id="一、远程分支"><a href="#一、远程分支" class="headerlink" title="一、远程分支"></a><font size=3>一、远程分支</font></h1><h2 id="1-远程分支说明"><a href="#1-远程分支说明" class="headerlink" title="1. 远程分支说明"></a><font size=3>1. 远程分支说明</font></h2><p>远程引用是对远程仓库的引用（指针），包括分支、标签等等。 我们可以通过 <code>git ls-remote &lt;remote&gt;</code> 来显式地获得远程引用的完整列表， 或者通过 <code>git remote show &lt;remote&gt;</code> 获得远程分支的更多信息。 然而，一个更常见的做法是利用远程跟踪分支。</p>
<p>远程跟踪分支是远程分支状态的引用。它们是我们无法移动的本地引用。一旦我们进行了网络通信， Git 就会为我们移动它们以精确反映远程仓库的状态。请将它们看做书签， 这样可以提醒我们该分支在远程仓库中的位置就是我们最后一次连接到它们的位置。</p>
<p>它们以 <code>&lt;remote&gt;/&lt;branch&gt;</code> 的形式命名。 例如，如果我们想要看你最后一次与远程仓库 <code>origin</code> 通信时 <code>master</code> 分支的状态，我们可以查看 <code>origin/master</code> 分支。 我们与同事合作解决一个问题并且他们推送了一个 <code>iss53</code> 分支，我们可能有自己的本地 <code>iss53</code> 分支， 然而在服务器上的分支会以 <code>origin/iss53</code> 来表示。</p>
<h2 id="2-远程分支实例"><a href="#2-远程分支实例" class="headerlink" title="2. 远程分支实例"></a><font size=3>2. 远程分支实例</font></h2><p>让我们来看一个例子。 假设我们的网络里有一个在 <code>git.ourcompany.com</code> 的 Git 服务器。 如果我们从这里克隆，Git 的 <code>clone</code> 命令会为我们自动将其命名为 <code>origin</code>，拉取它的所有数据， 创建一个指向它的 <code>master</code> 分支的指针，并且在本地将其命名为 <code>origin/master</code>。 Git 也会给我们一个与 origin 的 <code>master</code> 分支在指向同一个地方的本地 <code>master</code> 分支，这样我们就有工作的基础。</p>
<blockquote>
<p>“origin” 并无特殊含义</p>
<p>远程仓库名字 “origin” 与分支名字 “master” 一样，在 Git 中并没有任何特别的含义一样。 同时 “master” 是当我们运行 <code>git init</code> 时默认的起始分支名字，原因仅仅是它的广泛使用， “origin” 是当我们运行 <code>git clone</code> 时默认的远程仓库名字。 如果我们运行 <code>git clone -o booyah</code>，那么我们默认的远程分支名字将会是 <code>booyah/master</code>。</p>
</blockquote>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/remote-branches-1.png" alt="克隆之后的服务器与本地仓库。" style="zoom: 67%;" />

<p align="center">
    图一-1 克隆之后的服务器与本地仓库
</p>

<p>如果我们在本地的 <code>master</code> 分支做了一些工作，在同一段时间内有其他人推送提交到 <code>git.ourcompany.com</code> 并且更新了它的 <code>master</code> 分支，这就是说我们们和其他人的提交历史已走向不同的方向。 即便这样，只要我们保持不与 <code>origin</code> 服务器连接（并拉取数据），我们的 <code>origin/master</code> 指针就不会移动。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/remote-branches-2.png" alt="本地与远程的工作可以分叉。" style="zoom: 67%;" />

<p align="center">
    图一-2 本地与远程的工作可以分叉
</p>

<p>如果要与给定的远程仓库同步数据，运行 <code>git fetch &lt;remote&gt;</code> 命令（在本例中为 <code>git fetch origin</code>）。 这个命令查找 <code>origin</code> 是哪一个服务器（在本例中，它是 <code>git.ourcompany.com</code>）， 从中抓取本地没有的数据，并且更新本地数据库，移动 <code>origin/master</code> 指针到更新之后的位置。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/remote-branches-3.png" alt="`git fetch` 更新你的远程仓库引用。" style="zoom: 67%;" />

<p align="center">
    图一-3 git fetch 更新我们的远程跟踪分支
</p>

<p>为了演示有多个远程仓库与远程分支的情况，假定我们有另一个内部 Git 服务器，仅服务于我们的某个敏捷开发团队。 这个服务器位于 <code>git.team1.ourcompany.com</code>。 我们可以运行 <code>git remote add</code> 命令添加一个新的远程仓库引用到当前的项目。 将这个远程仓库命名为 <code>teamone</code>，将其作为完整 URL 的缩写。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/remote-branches-4.png" alt="添加另一个远程仓库。" style="zoom: 67%;" />

<p align="center">
    图一-4 远程跟踪分支 teamone/master
</p>

<h1 id="二、创建远程仓库"><a href="#二、创建远程仓库" class="headerlink" title="二、创建远程仓库"></a><font size=3>二、创建远程仓库</font></h1><p>其实在GitHub上创建的仓库有两种状态，一种是单纯的创建一个空仓库。另一种是创建的时候直接创建一些必要的文件，并进行一次提交。</p>
<h2 id="1-空的仓库"><a href="#1-空的仓库" class="headerlink" title="1. 空的仓库"></a><font size=3>1. 空的仓库</font></h2><h3 id="1-1-仓库说明"><a href="#1-1-仓库说明" class="headerlink" title="1.1 仓库说明"></a><font size=3>1.1 仓库说明</font></h3><p>需要注意的是这种仓库灵活性比较大，由于是空的，所以们可以直接将此仓库clone到本地使用，这样的话，clone下来的仓库直接与远程仓库建立好了连接，可以直接使用。或者我们也可以自己在本地初始化一个文件夹，然后将这个本地仓库与远程仓库建立连接，然后就可以开始呢使用了。</p>
<h3 id="1-2-创建空仓库"><a href="#1-2-创建空仓库" class="headerlink" title="1.2 创建空仓库"></a><font size=3>1.2 创建空仓库</font></h3><p>我们先创建一个空的远程仓库作为测试使用</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230624215053917.png" alt="image-20230624215053917" style="zoom:50%;" />

<p>按照上边填写信息，然后点击右下角的创建，我们会得到一个空的仓库，然后会弹出以下界面，在界面中会给出我们相关命令来提示如何操作仓库：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230624215338926.png" alt="image-20230624215338926" style="zoom:50%;" />

<h2 id="2-非空仓库"><a href="#2-非空仓库" class="headerlink" title="2. 非空仓库"></a><font size=3>2. 非空仓库</font></h2><h3 id="2-1-仓库说明"><a href="#2-1-仓库说明" class="headerlink" title="2.1 仓库说明"></a><font size=3>2.1 仓库说明</font></h3><p>这种仓库在创建的时候已经添加了readme文件或者其他的文件，并产生了一次提交，这种的一般是要clone到本地使用的，当然，也可以在本地初始化仓库，然后建立与远程仓库的连接，只不过可能会产生一些冲突，但是解决一下应该也问题不大。</p>
<h3 id="2-2-创建非空仓库"><a href="#2-2-创建非空仓库" class="headerlink" title="2.2 创建非空仓库"></a><font size=3>2.2 创建非空仓库</font></h3><p>我们其实也可以创建非空仓库，比如直接添加readme文件还有.gitignore忽略文件，以及license文件等，来看一下：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230624215703682.png" alt="image-20230624215703682" style="zoom:50%;" />

<p>这个直接创建一个初始化的仓库，好像必须要在这里勾线<code>ADD a README file</code>，然后我们点击创建，会看到以下界面：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230624215823917.png" alt="image-20230624215823917" style="zoom:50%;" />

<p>会发现直接就是仓库源码界面了，而且仓库直接进行了一次提交，班喊一个README.md文件。</p>
<h2 id="3-测试说明"><a href="#3-测试说明" class="headerlink" title="3. 测试说明"></a><font size=3>3. 测试说明</font></h2><p>这里只是演示一下两种仓库的创立方式，后边为了学习更多的操作，我们选择使用空仓库来进行演示说明。</p>
<h1 id="三、克隆仓库"><a href="#三、克隆仓库" class="headerlink" title="三、克隆仓库"></a><font size=3>三、克隆仓库</font></h1><p>接下来我们将仓库下载到本地，看一下情况。想要进行仓库的克隆，需要先让我们的电脑跟GitHub进行正常连接，我们是通过SSH连接的，在前边Git配置的时候我们其实已经配置过了，这里就不用管这一步了。</p>
<h2 id="1-git-clone命令详细说明"><a href="#1-git-clone命令详细说明" class="headerlink" title="1. git clone命令详细说明"></a><font size=3>1. git clone命令详细说明</font></h2><p>这个我们还是看文档吧：<a target="_blank" rel="noopener" href="https://git-scm.com/docs/git-clone/en#_name">Git - git-clone Documentation (git-scm.com)</a>，文档中说的特别详细。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">git clone [--template=&lt;template-directory&gt;]</span><br><span class="line">	  [-l] [-s] [--no-hardlinks] [-q] [-n] [--bare] [--mirror]</span><br><span class="line">	  [-o &lt;name&gt;] [-b &lt;name&gt;] [-u &lt;upload-pack&gt;] [--reference &lt;repository&gt;]</span><br><span class="line">	  [--dissociate] [--separate-git-dir &lt;git-dir&gt;]</span><br><span class="line">	  [--depth &lt;depth&gt;] [--[no-]single-branch] [--no-tags]</span><br><span class="line">	  [--recurse-submodules[=&lt;pathspec&gt;]] [--[no-]shallow-submodules]</span><br><span class="line">	  [--[no-]remote-submodules] [--jobs &lt;n&gt;] [--sparse] [--[no-]reject-shallow]</span><br><span class="line">	  [--filter=&lt;filter&gt; [--also-filter-submodules]] [--] &lt;repository&gt;</span><br><span class="line">	  [&lt;directory&gt;]</span><br></pre></td></tr></table></figure>

<h2 id="2-基本clone命令"><a href="#2-基本clone命令" class="headerlink" title="2. 基本clone命令"></a><font size=3>2. 基本clone命令</font></h2><h3 id="2-1-命令说明"><a href="#2-1-命令说明" class="headerlink" title="2.1 命令说明"></a><font size=3>2.1 命令说明</font></h3><p>我们想要将仓库下载到本地的话，可以用下边的命令：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_"># </span><span class="language-bash">克隆相关操作</span></span><br><span class="line">git clone &lt;repository_url&gt;  # clone仓库</span><br><span class="line">git clone &lt;repository_url&gt; another-repository-name # clone下来的仓库存储为其他名称</span><br></pre></td></tr></table></figure>

<ul>
<li>repository_url ：仓库的地址，这个怎么看？对于创建的空仓库，在提示界面会有，对于直接初始化的仓库，我们可以直接在源码界面找到，这里会有两种地址，一种是https的，一种是ssh地址，一般我是用的ssh地址，两者是有区别的，但是忘记了，不过简单验证一下就知道了，用ssh地址好像是更方便一些。如下图：</li>
</ul>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230624220607035.png" alt="image-20230624220607035" style="zoom:50%;" />

<ul>
<li>another-repository-name ：就是我们克隆到本地的仓库的名字是可以与远程仓库的名字不同的，这里就是clone到本地之后的仓库的名字。</li>
</ul>
<h3 id="2-2-使用实例"><a href="#2-2-使用实例" class="headerlink" title="2.2 使用实例"></a><font size=3>2.2 使用实例</font></h3><p>我们这里以前边创建的空仓库为例进行演示<a target="_blank" rel="noopener" href="https://github.com/sumumm/git-test">sumumm&#x2F;git-test (github.com)</a>。</p>
<h4 id="2-2-1-HTTPS地址"><a href="#2-2-1-HTTPS地址" class="headerlink" title="2.2.1 HTTPS地址"></a><font size=3>2.2.1 HTTPS地址</font></h4><ul>
<li>查看HTTPS地址</li>
</ul>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230624222045692.png" alt="image-20230624222045692" style="zoom:50%;" />

<ul>
<li>clone仓库</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git <span class="built_in">clone</span> https://github.com/sumumm/git-test.git http_addr_git_test</span></span><br><span class="line">Cloning into &#x27;http_addr_git_test&#x27;...</span><br><span class="line">warning: You appear to have cloned an empty repository.</span><br></pre></td></tr></table></figure>

<ul>
<li>查看文件夹内容：</li>
</ul>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230625063859546.png" alt="image-20230625063859546" style="zoom:50%;" />

<p>可以发现，这是一个空仓库，并且是有.git目录的。</p>
<h4 id="2-2-2-SSH地址"><a href="#2-2-2-SSH地址" class="headerlink" title="2.2.2 SSH地址"></a><font size=3>2.2.2 SSH地址</font></h4><ul>
<li>查看SSH地址</li>
</ul>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230624222444548.png" alt="image-20230624222444548" style="zoom:50%;" />

<ul>
<li>clone仓库</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git <span class="built_in">clone</span> git@github.com:sumumm/git-test.git ssh_addr_git_test</span></span><br><span class="line">Cloning into &#x27;ssh_addr_git_test&#x27;...</span><br><span class="line">warning: You appear to have cloned an empty repository.</span><br></pre></td></tr></table></figure>

<ul>
<li>查看文件夹内容：</li>
</ul>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230624222606888.png" alt="image-20230624222606888" style="zoom:50%;" />

<p>可以发现，这是一个空仓库，并且是有.git目录的。</p>
<h2 id="3-克隆指定版本"><a href="#3-克隆指定版本" class="headerlink" title="3. 克隆指定版本"></a><font size=3>3. 克隆指定版本</font></h2><p>那有的时候我们想要clone某个仓库指定的版本，该怎么操作？貌似不太支持啊，我看官方说明文档里也没得，那暂时就只有一种办法喽：</p>
<p>（1）克隆整个仓库下来；</p>
<p>（2）查看提交记录，获取要跳转的指定版本的commit提交记录的ID；</p>
<p>（3）版本回滚。</p>
<h2 id="4-克隆指定分支"><a href="#4-克隆指定分支" class="headerlink" title="4. 克隆指定分支"></a><font size=3>4. 克隆指定分支</font></h2><p>有的时候我们想要clone指定分支的话，怎么办？</p>
<h3 id="4-1-命令说明"><a href="#4-1-命令说明" class="headerlink" title="4.1 命令说明"></a><font size=3>4.1 命令说明</font></h3><p>一般来讲，我们不加任何参数的实行<code>git clone</code>，这样将会下载到master分支或者叫main分支，或者就是自己定义的默认的分支。我们可以加上<code>-b</code>参数克隆指定分支：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git clone -b &lt;branch_name&gt; &lt;repository_url&gt;  # clone仓库</span><br></pre></td></tr></table></figure>

<h3 id="4-2-使用实例"><a href="#4-2-使用实例" class="headerlink" title="4.2 使用实例"></a><font size=3>4.2 使用实例</font></h3><p>我们还是以hexo的仓库为例（<a target="_blank" rel="noopener" href="https://github.com/hexojs/hexo">hexojs&#x2F;hexo: A fast, simple &amp; powerful blog framework, powered by Node.js. (github.com)</a>）我们先看一下都有哪些分支：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230625070958747.png" alt="image-20230625070958747" style="zoom:50%;" />

<p>然后执行以下命令：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git <span class="built_in">clone</span> -b 4.2.1 git@github.com:hexojs/hexo.git</span></span><br><span class="line">Cloning into &#x27;hexo&#x27;...</span><br><span class="line">remote: Enumerating objects: 27265, done.</span><br><span class="line">remote: Counting objects: 100% (335/335), done.</span><br><span class="line">remote: Compressing objects: 100% (291/291), done.</span><br><span class="line">remote: Total 27265 (delta 98), reused 173 (delta 42), pack-reused 26930</span><br><span class="line">Receiving objects: 100% (27265/27265), 7.44 MiB | 3.80 MiB/s, done.</span><br><span class="line">Resolving deltas: 100% (17530/17530), done.</span><br></pre></td></tr></table></figure>

<p>然后我们来看一下分支名：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230625071210402.png" alt="image-20230625071210402" style="zoom:50%;" />

<p>可以看到这里的分支为4.2.1啦。 </p>
<h2 id="5-depth参数"><a href="#5-depth参数" class="headerlink" title="5. depth参数"></a><font size=3>5. depth参数</font></h2><p>我们经常会用 git clone来下载项目，但遇到大项目的时候，clone 就很慢，比如我要学习的uboot的有一个分支<a target="_blank" rel="noopener" href="https://github.com/nxp-imx/uboot-imx/tree/nxp/imx_v2016.03_4.1.15_2.0.0_ga">nxp-imx&#x2F;uboot-imx at nxp&#x2F;imx_v2016.03_4.1.15_2.0.0_ga (github.com)</a>：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20241025072136813.png" alt="image-20241025072136813" />

<p>在clone的时候就很慢很慢很慢。原因是历史提交的commits太多了，我们能不能只下载其中的一个？当然可以啦，就是通过depth参数控制：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git clone git@github.com:nxp-imx/uboot-imx.git -b nxp/imx_v2016.03_4.1.15_2.0.0_ga --depth 1</span><br></pre></td></tr></table></figure>

<p>就会发现下载的很快很快，并且只有一次提交：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20241025072458099.png" alt="image-20241025072458099" />

<p>那这样代码还是全的么？当然，代码是最新的完整代码。那为啥下载的内容少了呢？少了哪一部分呢？就是历史 commit。</p>
<p>git 中文件是通过 object 存储不同数据的：</p>
<ul>
<li>blob 对象存储文件内容</li>
<li>tree 对象存储文件路径</li>
<li>commit 对象存储 commit 信息，关联多个 tree</li>
</ul>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/v2-dd3f82b802cf5f09a024a7c6f0ffe694_b.jpg" alt="img" />

<p>然后 HEAD、branch、tag 等是指向具体 commit 的指针，可以在 .git&#x2F;refs 下看到。所以说，每个版本的代码都是从 commit 对象作为入口关联起来的。指定了 depth 1 的时候，就是只保留了最新的入口，历史入口就没下载了。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/v2-e45c2455917162e1095eb497dd08766a_b.jpg" alt="img" />

<p>这样自然快很多，代码也是完整的。但这么好的事情也是有代价的，它有一些后遗症。</p>
<p>（1）最容易想到的就是切不到历史 commit，不能使用git reset命令去切换到其他提交记录。</p>
<p>（2）git pull 的时候，也下载不了历史 commit 的代码。git 团队自然也想到了这点，于是提供了一个 unshallow 的选项，加上 –unshallow 再 pull 的时候也会同时拉取历史 commit（默认没开这个是为了性能）。</p>
<p>我们完全可以用 depth 1 下载的项目来开发，正常的 pull、push 都没问题，因为都是基于最新 commit 创建的更新的 commit。当你有一天需要历史 commit 的时候再 pull –unshallow 也不迟。</p>
<p>（3）换不了其他 branch。切不到别的分支是因为 fetch 配置导致的，配置成 <code>+refs/heads/**:refs/remotes/origin/** </code>也就可以了，也就是拉取远程所有分支代码到本地。这样再 fetch 和 pull 就会拉取所有分支的新 commit，也可以正常的切分支。</p>
<p>总的来说–depth 1 在下载大项目的时候，或者 build 时下载代码的时候，都很有意义。它提高下载速度导致的俩后遗症也都可以解决。</p>
<h1 id="四、本地与远程仓库的联系？"><a href="#四、本地与远程仓库的联系？" class="headerlink" title="四、本地与远程仓库的联系？"></a><font size=3>四、本地与远程仓库的联系？</font></h1><h2 id="1-概述"><a href="#1-概述" class="headerlink" title="1. 概述"></a><font size=3>1. 概述</font></h2><h3 id="1-1-情况说明"><a href="#1-1-情况说明" class="headerlink" title="1.1 情况说明"></a><font size=3>1.1 情况说明</font></h3><p>上边我们学习了如何从GitHub克隆仓库，这样我们可以直接得到一个初始化过的仓库，那么，倘若我们本地有一个仓库，我们已经做了很多的提交了，但是它与远程仓库没有任何关系，我们该如何做才能将本地仓库与远程的仓库联系起来？</p>
<h3 id="1-2-命令汇总"><a href="#1-2-命令汇总" class="headerlink" title="1.2 命令汇总"></a><font size=3>1.2 命令汇总</font></h3><p>这里的详细命令我们可以参考<a target="_blank" rel="noopener" href="https://git-scm.com/docs/git-remote#_name">Git - git-remote Documentation (git-scm.com)</a>：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">git remote [-v | --verbose]</span><br><span class="line">git remote add [-t &lt;branch&gt;] [-m &lt;master&gt;] [-f] [--[no-]tags] [--mirror=(fetch|push)] &lt;name&gt; &lt;URL&gt;</span><br><span class="line">git remote rename [--[no-]progress] &lt;old&gt; &lt;new&gt;</span><br><span class="line">git remote remove &lt;name&gt;</span><br><span class="line">git remote set-head &lt;name&gt; (-a | --auto | -d | --delete | &lt;branch&gt;)</span><br><span class="line">git remote set-branches [--add] &lt;name&gt; &lt;branch&gt;…​</span><br><span class="line">git remote get-url [--push] [--all] &lt;name&gt;</span><br><span class="line">git remote set-url [--push] &lt;name&gt; &lt;newurl&gt; [&lt;oldurl&gt;]</span><br><span class="line">git remote set-url --add [--push] &lt;name&gt; &lt;newurl&gt;</span><br><span class="line">git remote set-url --delete [--push] &lt;name&gt; &lt;URL&gt;</span><br><span class="line">git remote [-v | --verbose] show [-n] &lt;name&gt;…​</span><br><span class="line">git remote prune [-n | --dry-run] &lt;name&gt;…​</span><br><span class="line">git remote [-v | --verbose] update [-p | --prune] [(&lt;group&gt; | &lt;remote&gt;)…​]</span><br></pre></td></tr></table></figure>

<p>上边这么多命令，如何使用呢？其实我们创建空仓库的时候就已经告诉我们了：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230625193740801.png" alt="image-20230625193740801" style="zoom:50%;" />

<p>接下来就来详细学习一下吧。</p>
<h2 id="2-本地仓库准备"><a href="#2-本地仓库准备" class="headerlink" title="2. 本地仓库准备"></a><font size=3>2. 本地仓库准备</font></h2><h3 id="2-1-仓库说明-1"><a href="#2-1-仓库说明-1" class="headerlink" title="2.1 仓库说明"></a><font size=3>2.1 仓库说明</font></h3><p>为了演示一些情况，我们在这里创建一个本地仓库，这个仓库有以下特点：</p>
<p>（1）含有两个分支，分别为master和dev分支。</p>
<p>（2）master分支最开始做两次修改和提交，分别为C0、C1。</p>
<p>（3）master完成C1提交的时候，我们创建dev分支。</p>
<p>（4）我们在master分支在进行一次C2提交。</p>
<p>（5）切换到dev分支，然后进行一次C3提交。</p>
<p>其中每次提交只要有修改即可，我们的这个仓库包含read.md文件和test.txt文件。</p>
<h3 id="2-2-本地仓库创建"><a href="#2-2-本地仓库创建" class="headerlink" title="2.2 本地仓库创建"></a><font size=3>2.2 本地仓库创建</font></h3><ul>
<li>（1）初始化仓库，在master分支进行C0提交</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash"><span class="built_in">mkdir</span> remote-branch</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash"><span class="built_in">cd</span> remote-branch/</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git init</span></span><br><span class="line">Initialized empty Git repository in D:/git_test/remote-branch/.git/</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash"><span class="built_in">touch</span> readme.md test.txt</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git add .</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git commit -m <span class="string">&quot;feat:C0提交&quot;</span></span></span><br><span class="line">[master e2347e1] feat:C0提交</span><br><span class="line"> Date: Sun Jun 25 19:24:55 2023 +0800</span><br><span class="line"> 2 files changed, 0 insertions(+), 0 deletions(-)</span><br><span class="line"> create mode 100644 readme.md</span><br><span class="line"> create mode 100644 test.txt</span><br></pre></td></tr></table></figure>

<ul>
<li>（2）在master分支进行C1提交，并创建dev分支</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git commit -a -m <span class="string">&quot;feat:C1提交&quot;</span></span></span><br><span class="line">[master 61da57c] feat:C1提交</span><br><span class="line"> 2 files changed, 2 insertions(+)</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git branch dev</span></span><br></pre></td></tr></table></figure>

<ul>
<li>（3）在master分支进行C2提交</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git commit -a -m <span class="string">&quot;feat:C2提交&quot;</span></span></span><br><span class="line">[master 0afb95f] feat:C2提交</span><br><span class="line"> 2 files changed, 4 insertions(+), 2 deletions(-)</span><br></pre></td></tr></table></figure>

<ul>
<li>（4）切换到dev分支，并进行dev分支的C3提交</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git checkout dev</span> </span><br><span class="line">Switched to branch &#x27;dev&#x27;</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git commit -a -m <span class="string">&quot;feat:C3提交(dev分支)&quot;</span></span></span><br><span class="line">[dev 649cd89] feat:C3提交(dev分支)</span><br><span class="line"> 2 files changed, 4 insertions(+), 2 deletions(-)</span><br></pre></td></tr></table></figure>

<h3 id="2-3-两个分支情况"><a href="#2-3-两个分支情况" class="headerlink" title="2.3 两个分支情况"></a><font size=3>2.3 两个分支情况</font></h3><h4 id="2-3-1-master分支"><a href="#2-3-1-master分支" class="headerlink" title="2.3.1 master分支"></a><font size=3>2.3.1 master分支</font></h4><p>master分支的文件内容及提交记录如下：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230625193202012.png" alt="image-20230625193202012" style="zoom: 80%;" />

<h4 id="2-3-2-dev分支"><a href="#2-3-2-dev分支" class="headerlink" title="2.3.2 dev分支"></a><font size=3>2.3.2 dev分支</font></h4><p>dev分支的文件内容及提交记录如下：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230625193310502.png" alt="image-20230625193310502" style="zoom: 80%;" />

<h2 id="3-添加本地仓库与远程仓库连接"><a href="#3-添加本地仓库与远程仓库连接" class="headerlink" title="3. 添加本地仓库与远程仓库连接"></a><font size=3>3. 添加本地仓库与远程仓库连接</font></h2><h3 id="3-1-命令说明"><a href="#3-1-命令说明" class="headerlink" title="3.1 命令说明"></a><font size=3>3.1 命令说明</font></h3><p>想要让本地仓库与远程仓库建立连接，我们可以使用下边的命令：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git remote add &lt;remote_name&gt; &lt;URL&gt;</span><br></pre></td></tr></table></figure>

<ul>
<li>remote_name ：表示远程分支在本地的名字，我们一般命名为origin。</li>
<li>URL ：表示远程仓库的链接，我一般是使用的SSH链接，HTTPS的也可以。</li>
</ul>
<h3 id="3-2-使用实例"><a href="#3-2-使用实例" class="headerlink" title="3.2 使用实例"></a><font size=3>3.2 使用实例</font></h3><p>我们在刚才的仓库的master分支执行下边的命令：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git remote add origin git@github.com:sumumm/git-test.git</span><br></pre></td></tr></table></figure>

<p>会发现什么输出都没有，怎么看有事没有建立成功？我们后边再说。</p>
<h2 id="4-查看远程引用的信息"><a href="#4-查看远程引用的信息" class="headerlink" title="4. 查看远程引用的信息"></a><font size=3>4. 查看远程引用的信息</font></h2><h3 id="4-1-命令说明-1"><a href="#4-1-命令说明-1" class="headerlink" title="4.1 命令说明"></a><font size=3>4.1 命令说明</font></h3><p>我们刚才是建立了本地仓库与远程仓库的连接，但是我们如何获取这些信息呢？</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">git remote show &lt;remote_name&gt;</span><br><span class="line">git remote -v</span><br><span class="line">git remote</span><br></pre></td></tr></table></figure>

<h3 id="4-2-使用实例-1"><a href="#4-2-使用实例-1" class="headerlink" title="4.2 使用实例"></a><font size=3>4.2 使用实例</font></h3><p>我们接下来看一下这三条命令显示的信息有何不同：</p>
<ul>
<li>（1）<code>git remote show &lt;remote_name&gt;</code></li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git remote show origin</span> </span><br><span class="line">* remote origin</span><br><span class="line">  Fetch URL: git@github.com:sumumm/git-test.git</span><br><span class="line">  Push  URL: git@github.com:sumumm/git-test.git</span><br><span class="line">  HEAD branch: (unknown)</span><br></pre></td></tr></table></figure>

<ul>
<li>（2）<code>git remote -v</code></li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git remote -v</span> </span><br><span class="line">origin  git@github.com:sumumm/git-test.git (fetch)</span><br><span class="line">origin  git@github.com:sumumm/git-test.git (push)</span><br></pre></td></tr></table></figure>

<ul>
<li>（3）<code>git remote</code></li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git remote</span></span><br><span class="line">origin</span><br></pre></td></tr></table></figure>

<h3 id="4-3-多个remote名？"><a href="#4-3-多个remote名？" class="headerlink" title="4.3 多个remote名？"></a><font size=3>4.3 多个remote名？</font></h3><p>有个想法，remote_name可以有多个吗？它们都指向同一个git远程仓库地址，我们来尝试一下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git remote add aaa git@github.com:sumumm/git-test.git</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git remote add bbb git@github.com:sumumm/git-test.git</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git remote</span></span><br><span class="line">aaa</span><br><span class="line">bbb</span><br><span class="line">origin</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git remote -v</span></span><br><span class="line">aaa     git@github.com:sumumm/git-test.git (fetch)</span><br><span class="line">aaa     git@github.com:sumumm/git-test.git (push)</span><br><span class="line">bbb     git@github.com:sumumm/git-test.git (fetch)</span><br><span class="line">bbb     git@github.com:sumumm/git-test.git (push)</span><br><span class="line">origin  git@github.com:sumumm/git-test.git (fetch)</span><br><span class="line">origin  git@github.com:sumumm/git-test.git (push)</span><br></pre></td></tr></table></figure>

<p>可以看到是可以的，但是这样做暂时似乎没发现有什么意义。</p>
<h2 id="5-移除与远程仓库的连接"><a href="#5-移除与远程仓库的连接" class="headerlink" title="5. 移除与远程仓库的连接"></a><font size=3>5. 移除与远程仓库的连接</font></h2><p>我们上边建立了那么多的多余的remote名，怎么移除？</p>
<h3 id="5-1-命令说明"><a href="#5-1-命令说明" class="headerlink" title="5.1 命令说明"></a><font size=3>5.1 命令说明</font></h3><p>我们可以通过以下命令移除本地仓库与远程仓库的关联，就相当于删除了本地的remote_name：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git remote remove &lt;remote_name&gt;         # 删除本地仓库与远程仓库关联</span><br></pre></td></tr></table></figure>

<h3 id="5-2-使用实例"><a href="#5-2-使用实例" class="headerlink" title="5.2 使用实例"></a><font size=3>5.2 使用实例</font></h3><p>我们执行以下命令：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git remote remove aaa</span> </span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git remote remove bbb</span> </span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git remote -v</span></span><br><span class="line">origin  git@github.com:sumumm/git-test.git (fetch)</span><br><span class="line">origin  git@github.com:sumumm/git-test.git (push)</span><br></pre></td></tr></table></figure>

<p>会发现前边创建的aaa，bbb等远程连接的名就被删除了，当我们删除最后一个与远程仓库的连接的时候，就意味着我们本地仓库断开了与远程仓库的连接，本地也便无法再跟踪远程仓库了。</p>
<h1 id="五、推送到远程仓库"><a href="#五、推送到远程仓库" class="headerlink" title="五、推送到远程仓库"></a><font size=3>五、推送到远程仓库</font></h1><p>上边我们已经可以建立与远程仓库的连接了，那我们怎么吧本地的仓库推送到远程仓库？</p>
<h2 id="1-概述-1"><a href="#1-概述-1" class="headerlink" title="1. 概述"></a><font size=3>1. 概述</font></h2><p>我们可以在这里看到相关的命令说明：<a target="_blank" rel="noopener" href="https://git-scm.com/docs/git-push">Git - git-push Documentation (git-scm.com)</a></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">git push [--all | --branches | --mirror | --tags] [--follow-tags] [--atomic] [-n | --dry-run] [--receive-pack=&lt;git-receive-pack&gt;]</span><br><span class="line">	   [--repo=&lt;repository&gt;] [-f | --force] [-d | --delete] [--prune] [-v | --verbose]</span><br><span class="line">	   [-u | --set-upstream] [-o &lt;string&gt; | --push-option=&lt;string&gt;]</span><br><span class="line">	   [--[no-]signed|--signed=(true|false|if-asked)]</span><br><span class="line">	   [--force-with-lease[=&lt;refname&gt;[:&lt;expect&gt;]] [--force-if-includes]]</span><br><span class="line">	   [--no-verify] [&lt;repository&gt; [&lt;refspec&gt;…]]</span><br></pre></td></tr></table></figure>

<p>其实在创建空仓库的时候GitHub也告诉了我们最基础的<code>git push</code>命令。</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230625200311770.png" alt="image-20230625200311770" style="zoom:50%;" />

<h2 id="2-推送到远程仓库"><a href="#2-推送到远程仓库" class="headerlink" title="2. 推送到远程仓库"></a><font size=3>2. 推送到远程仓库</font></h2><h3 id="2-1-命令说明-1"><a href="#2-1-命令说明-1" class="headerlink" title="2.1 命令说明"></a><font size=3>2.1 命令说明</font></h3><p>我们来看一下基础的命令：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git push -u &lt;remote_name&gt; &lt;branch_name&gt; # 将本地仓库文件上传到远程仓库分支</span><br></pre></td></tr></table></figure>

<ul>
<li>remote_name ：远程仓库在本地的别名，我上边命名为了origin。</li>
<li>branch_name ：要推送的本地仓库的分支名。</li>
</ul>
<h3 id="2-2-使用实例-1"><a href="#2-2-使用实例-1" class="headerlink" title="2.2 使用实例"></a><font size=3>2.2 使用实例</font></h3><p>我们来将刚才本地的master分支和dev分支都推送到远程仓库：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git push -u origin master</span></span><br><span class="line">Enumerating objects: 9, done.</span><br><span class="line">Counting objects: 100% (9/9), done.</span><br><span class="line">Delta compression using up to 16 threads</span><br><span class="line">Compressing objects: 100% (6/6), done.</span><br><span class="line">Writing objects: 100% (9/9), 743 bytes | 371.00 KiB/s, done.</span><br><span class="line">Total 9 (delta 0), reused 0 (delta 0), pack-reused 0</span><br><span class="line">To github.com:sumumm/git-test.git</span><br><span class="line"> * [new branch]      master -&gt; master</span><br><span class="line">branch &#x27;master&#x27; set up to track &#x27;origin/master&#x27;.</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git push -u origin dev</span></span><br><span class="line">Enumerating objects: 5, done.</span><br><span class="line">Counting objects: 100% (5/5), done.</span><br><span class="line">Delta compression using up to 16 threads</span><br><span class="line">Compressing objects: 100% (2/2), done.</span><br><span class="line">Writing objects: 100% (3/3), 308 bytes | 308.00 KiB/s, done.</span><br><span class="line">Total 3 (delta 0), reused 0 (delta 0), pack-reused 0</span><br><span class="line">remote: </span><br><span class="line">remote: Create a pull request for &#x27;dev&#x27; on GitHub by visiting:</span><br><span class="line">remote:      https://github.com/sumumm/git-test/pull/new/dev</span><br><span class="line">remote:</span><br><span class="line">To github.com:sumumm/git-test.git</span><br><span class="line"> * [new branch]      dev -&gt; dev</span><br><span class="line">branch &#x27;dev&#x27; set up to track &#x27;origin/dev&#x27;.</span><br></pre></td></tr></table></figure>

<p>然后我们再来看一下GitHub远程仓库（<a target="_blank" rel="noopener" href="https://github.com/sumumm/git-test">sumumm&#x2F;git-test (github.com)</a>）的情况：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230625200752434.png" alt="image-20230625200752434" style="zoom:50%;" />

<p>若是看每个分支的历史记录的话，会发现，它们的提交与本地一模一样，这里就不贴图了。</p>
<h2 id="3-一个问题"><a href="#3-一个问题" class="headerlink" title="3. 一个问题"></a><font size=3>3. 一个问题</font></h2><p>有这样一种情况，就是，远程分支比本地的提交更新的时候怎么办？或者说，本地提交与远程完全不一样怎么办？这两种情况其实都是一样的结果，我们可以尝试一下，我们刚才已经成功将master分支和dev分支推送到GitHub上的空仓库了，我们现在来修改本地仓库，我们在master分支做操作，我们重新修改master分支的C3提交，我们这里为了方便，近仅仅修改提交记录，也能达到一样的效果：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230625201323376.png" alt="image-20230625201323376" style="zoom:50%;" />

<p>然后我们再来推送试一下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git push -u origin master</span> </span><br><span class="line">To github.com:sumumm/git-test.git</span><br><span class="line"> ! [rejected]        master -&gt; master (non-fast-forward)</span><br><span class="line">error: failed to push some refs to &#x27;github.com:sumumm/git-test.git&#x27;</span><br><span class="line">hint: Updates were rejected because the tip of your current branch is behind</span><br><span class="line">hint: its remote counterpart. Integrate the remote changes (e.g.</span><br><span class="line">hint: &#x27;git pull ...&#x27;) before pushing again.</span><br><span class="line">hint: See the &#x27;Note about fast-forwards&#x27; in &#x27;git push --help&#x27; for details.</span><br></pre></td></tr></table></figure>

<p>发现它报错啦，这种情况怎么处理？我们后边再说。</p>
<h2 id="4-强制推送"><a href="#4-强制推送" class="headerlink" title="4. 强制推送"></a><font size=3>4. 强制推送</font></h2><p>上边我们分析了一些问题，就是推送失败的情况，其实很多时候都会这样，我们该怎么办？</p>
<h3 id="4-1-命令说明-2"><a href="#4-1-命令说明-2" class="headerlink" title="4.1 命令说明"></a><font size=3>4.1 命令说明</font></h3><p>我们可以加上<code>-f(--force)</code>参数：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git push --force -u &lt;remote_name&gt; &lt;branch_name&gt; # 将本地仓库文件上传到远程仓库分支</span><br></pre></td></tr></table></figure>

<p>这个命令会强制推送本地的所有提交记录到远程，就像给远程仓库的提交记录清空，然后重新推送了一样。</p>
<h3 id="4-2-使用实例-2"><a href="#4-2-使用实例-2" class="headerlink" title="4.2 使用实例"></a><font size=3>4.2 使用实例</font></h3><p>我们加上<code>--force</code>参数再试一下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git push --force -u origin master</span></span><br><span class="line">Enumerating objects: 5, done.</span><br><span class="line">Counting objects: 100% (5/5), done.</span><br><span class="line">Delta compression using up to 16 threads</span><br><span class="line">Compressing objects: 100% (2/2), done.</span><br><span class="line">Writing objects: 100% (3/3), 317 bytes | 45.00 KiB/s, done.</span><br><span class="line">Total 3 (delta 0), reused 0 (delta 0), pack-reused 0</span><br><span class="line">To github.com:sumumm/git-test.git</span><br><span class="line"> + 0afb95f...d547f1d master -&gt; master (forced update)</span><br><span class="line">branch &#x27;master&#x27; set up to track &#x27;origin/master&#x27;.</span><br></pre></td></tr></table></figure>

<p>会发现强制推送成功，这个时候再看GitHub远程仓库的话没回发现master分支的提交记录与本地完全一样啦。</p>
<h2 id="5-删除远程分支"><a href="#5-删除远程分支" class="headerlink" title="5. 删除远程分支"></a><font size=3>5. 删除远程分支</font></h2><p>额，在手册上看到的，但从来没遇到过使用的情况，这里也学习一下吧。</p>
<h3 id="5-1-命令说明-1"><a href="#5-1-命令说明-1" class="headerlink" title="5.1 命令说明"></a><font size=3>5.1 命令说明</font></h3><p>假设我们已经通过远程分支做完所有的工作了——也就是说我们和我们的协作者已经完成了一个特性， 并且将其合并到了远程仓库的 <code>master</code> 分支（或任何其他稳定代码分支）。 可以运行带有 <code>--delete</code> 选项的 <code>git push</code> 命令来删除一个远程分支。 我们可以通过以下命令在本地删除远程仓库的某一分支：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git push origin --delete &lt;remote_branch_name&gt;</span><br></pre></td></tr></table></figure>

<p>基本上这个命令做的只是从服务器上移除这个指针。 Git 服务器通常会保留数据一段时间直到垃圾回收运行，所以如果不小心删除掉了，通常是很容易恢复的。</p>
<h3 id="5-2-使用实例-1"><a href="#5-2-使用实例-1" class="headerlink" title="5.2 使用实例"></a><font size=3>5.2 使用实例</font></h3><p>我们在刚才的仓库的基础上删除dev分支：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git push origin --delete dev</span></span><br><span class="line">To github.com:sumumm/git-test.git</span><br><span class="line"> - [deleted]         dev</span><br></pre></td></tr></table></figure>

<p>我们来看一下远程仓库：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230625205412224.png" alt="image-20230625205412224" style="zoom:50%;" />

<p>发现我们的远程仓库中的dev分支消失了，那完全消失了吗？其实没有，如果我们没有从我们的本地仓库删除这个分支，并且我们有推送到 GitHub 的权限，那我们可以通过再次推送它而在 Github 上恢复它。</p>
<h1 id="六、从远程仓库拉取代码"><a href="#六、从远程仓库拉取代码" class="headerlink" title="六、从远程仓库拉取代码"></a><font size=3>六、从远程仓库拉取代码</font></h1><h2 id="1-概述-2"><a href="#1-概述-2" class="headerlink" title="1. 概述"></a><font size=3>1. 概述</font></h2><p>拉取就是将远程仓库的更改同步到本地仓库。</p>
<h3 id="1-1-命令汇总"><a href="#1-1-命令汇总" class="headerlink" title="1.1 命令汇总"></a><font size=3>1.1 命令汇总</font></h3><p>我们可以在这里看到相关的命令说明：<a target="_blank" rel="noopener" href="https://git-scm.com/docs/git-pull#_name">Git - git-pull Documentation (git-scm.com)</a></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git pull [&lt;options&gt;] [&lt;repository&gt; [&lt;refspec&gt;...]]</span><br></pre></td></tr></table></figure>

<h3 id="1-2-仓库准备"><a href="#1-2-仓库准备" class="headerlink" title="1.2 仓库准备"></a><font size=3>1.2 仓库准备</font></h3><p>我们再克隆一份测试仓库：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git <span class="built_in">clone</span> git@github.com:sumumm/git-test.git git-pull-test</span></span><br><span class="line">Cloning into &#x27;git-pull-test&#x27;...</span><br><span class="line">remote: Enumerating objects: 12, done.</span><br><span class="line">remote: Counting objects: 100% (12/12), done.</span><br><span class="line">remote: Compressing objects: 100% (8/8), done.</span><br><span class="line">remote: Total 12 (delta 0), reused 12 (delta 0), pack-reused 0</span><br><span class="line">Receiving objects: 100% (12/12), done.</span><br></pre></td></tr></table></figure>

<p>然后我们就得到了一份与远程仓库一模一样的本地仓库，我们进入刚的仓库在master分支做一次提交，修改完后如下所示：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230625211216587.png" alt="image-20230625211216587" style="zoom: 80%;" />

<p>我们将该动推送到远程仓库：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git push -u origin master</span> </span><br><span class="line">Enumerating objects: 5, done.</span><br><span class="line">Counting objects: 100% (5/5), done.</span><br><span class="line">Delta compression using up to 16 threads</span><br><span class="line">Compressing objects: 100% (3/3), done.</span><br><span class="line">Writing objects: 100% (3/3), 335 bytes | 335.00 KiB/s, done.</span><br><span class="line">Total 3 (delta 0), reused 0 (delta 0), pack-reused 0</span><br><span class="line">To github.com:sumumm/git-test.git</span><br><span class="line">   d547f1d..8e8c2e3  master -&gt; master</span><br><span class="line">branch &#x27;master&#x27; set up to track &#x27;origin/master&#x27;.</span><br></pre></td></tr></table></figure>

<p>我们来看一下远程仓库是否已经有了更新：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230625211355932.png" alt="image-20230625211355932" style="zoom:50%;" />

<h2 id="2-更新本地仓库"><a href="#2-更新本地仓库" class="headerlink" title="2. 更新本地仓库"></a><font size=3>2. 更新本地仓库</font></h2><h3 id="2-1-命令说明-2"><a href="#2-1-命令说明-2" class="headerlink" title="2.1 命令说明"></a><font size=3>2.1 命令说明</font></h3><p>我们执行以下命令可以更新本地仓库：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git pull &lt;remote_name&gt; &lt;远程分支名&gt;       # 从远程仓库更新本地仓库版本,远程分支与本地当前分支合并</span><br></pre></td></tr></table></figure>

<p>这个时候会将远程分支强行合并到本地当前所处的分支，此时可能会有冲突出现，解决后提交即可。</p>
<h3 id="2-2-使用实例-2"><a href="#2-2-使用实例-2" class="headerlink" title="2.2 使用实例"></a><font size=3>2.2 使用实例</font></h3><p>我们回到最开始的测试仓库remote-branch，为了演示冲突，我们将远程仓库合并到本地的dev分支。我们查看当前仓库dev分支的提交：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230625211603428.png" alt="image-20230625211603428" style="zoom:50%;" />

<p>然后我们执行以下命令，拉取master分支：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git pull origin master</span> </span><br><span class="line">remote: Enumerating objects: 5, done.</span><br><span class="line">remote: Counting objects: 100% (5/5), done.</span><br><span class="line">remote: Compressing objects: 100% (3/3), done.</span><br><span class="line">remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0</span><br><span class="line">Unpacking objects: 100% (3/3), 315 bytes | 63.00 KiB/s, done.</span><br><span class="line">From github.com:sumumm/git-test</span><br><span class="line"> * branch            master     -&gt; FETCH_HEAD</span><br><span class="line">   d547f1d..8e8c2e3  master     -&gt; origin/master</span><br><span class="line">Auto-merging readme.md</span><br><span class="line">CONFLICT (content): Merge conflict in readme.md</span><br><span class="line">Auto-merging test.txt</span><br><span class="line">CONFLICT (content): Merge conflict in test.txt</span><br><span class="line">Automatic merge failed; fix conflicts and then commit the result.</span><br></pre></td></tr></table></figure>

<p>发现，出现了报错，我们看一下文件：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230625211742766.png" alt="image-20230625211742766" style="zoom:50%;" />

<p>原来是发生了冲突，我们解决一下冲突，然后提交一次，再查看提交记录：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230625212229039.png" alt="image-20230625212229039" style="zoom:50%;" />

<p>现在我们切换到master分支，拉取一下master分支看看：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230625212418890.png" alt="image-20230625212418890" style="zoom:50%;" />

<h1 id="七、跟踪分支"><a href="#七、跟踪分支" class="headerlink" title="七、跟踪分支"></a><font size=3>七、跟踪分支</font></h1><h2 id="1-概述-3"><a href="#1-概述-3" class="headerlink" title="1. 概述"></a><font size=3>1. 概述</font></h2><p>从一个远程跟踪分支检出一个本地分支会自动创建所谓的“跟踪分支”（它跟踪的分支叫做“上游分支”）。 跟踪分支是与远程分支有直接关系的本地分支。 如果在一个跟踪分支上输入 <code>git pull</code>，Git 能自动地识别去哪个服务器上抓取、合并到哪个分支。</p>
<p>当克隆一个仓库时，它通常会自动地创建一个跟踪 <code>origin/master</code> 的 <code>master</code> 分支。 然而，如果我们愿意的话可以设置其他的跟踪分支，或是一个在其他远程仓库上的跟踪分支，又或者不跟踪 <code>master</code> 分支。</p>
<h2 id="2-切换分支"><a href="#2-切换分支" class="headerlink" title="2. 切换分支"></a><font size=3>2. 切换分支</font></h2><h3 id="2-1-命令说明-3"><a href="#2-1-命令说明-3" class="headerlink" title="2.1 命令说明"></a><font size=3>2.1 命令说明</font></h3><p>其实我们clone仓库的时候，会自动创建跟踪分支，我们通过<code>git checkout</code>命令就可以在本地创建与远程仓库一模一样的分支，并且建立跟踪关系</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git checkout -b &lt;local_branch_name&gt; &lt;remote_name&gt;/&lt;remote_branch_name&gt; </span><br></pre></td></tr></table></figure>

<h3 id="2-2-使用实例-3"><a href="#2-2-使用实例-3" class="headerlink" title="2.2 使用实例"></a><font size=3>2.2 使用实例</font></h3><p>我们来看一下，这里以hexo框架（<a target="_blank" rel="noopener" href="https://github.com/hexojs/hexo">hexojs&#x2F;hexo: A fast, simple &amp; powerful blog framework, powered by Node.js. (github.com)</a>）为例。我们将其克隆到本地，并查看克隆后的分支情况：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git <span class="built_in">clone</span> git@github.com:hexojs/hexo.git</span></span><br><span class="line">Cloning into &#x27;hexo&#x27;...</span><br><span class="line">remote: Enumerating objects: 27265, done.</span><br><span class="line">remote: Counting objects: 100% (335/335), done.</span><br><span class="line">remote: Compressing objects: 100% (293/293), done.</span><br><span class="line">remote: Total 27265 (delta 98), reused 173 (delta 40), pack-reused 26930</span><br><span class="line">Receiving objects: 100% (27265/27265), 7.44 MiB | 3.63 MiB/s, done.</span><br><span class="line">Resolving deltas: 100% (17527/17527), done.</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash"><span class="built_in">cd</span> hexo/</span></span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git branch</span> </span><br><span class="line">* master</span><br></pre></td></tr></table></figure>

<p>会发现只有一条分支，我们知道hexo框架是有很多分支的：</p>
<img data-src="https://fanhua-picture.oss-cn-hangzhou.aliyuncs.com/02%E5%BC%80%E5%8F%91%E5%B7%A5%E5%85%B7/03%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6/LV01-Git-05-%E8%BF%9C%E7%A8%8B%E4%BB%93%E5%BA%93-02-%E8%BF%9C%E7%A8%8B%E5%88%86%E6%94%AF/img/image-20230625213048886.png" alt="image-20230625213048886" style="zoom:50%;" />

<p>我们以这里的4.2.1分支为例，我们直接切换：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git checkout -b 4.2.1 origin/4.2.1</span> </span><br><span class="line">Switched to a new branch &#x27;4.2.1&#x27;</span><br><span class="line">branch &#x27;4.2.1&#x27; set up to track &#x27;origin/4.2.1&#x27;.</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git branch</span> </span><br><span class="line">* 4.2.1</span><br><span class="line">  master</span><br></pre></td></tr></table></figure>

<h2 id="3-查看设置的跟踪分支"><a href="#3-查看设置的跟踪分支" class="headerlink" title="3. 查看设置的跟踪分支"></a><font size=3>3. 查看设置的跟踪分支</font></h2><h3 id="3-1-命令说明-1"><a href="#3-1-命令说明-1" class="headerlink" title="3.1 命令说明"></a><font size=3>3.1 命令说明</font></h3><p>如果想要查看设置的所有跟踪分支，可以使用 <code>git branch</code> 的 <code>-vv</code> 选项。 这会将所有的本地分支列出来并且包含更多的信息，如每一个分支正在跟踪哪个远程分支与本地分支是否是领先、落后或是都有。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git branch -vv</span><br></pre></td></tr></table></figure>

<h3 id="3-2-使用实例-1"><a href="#3-2-使用实例-1" class="headerlink" title="3.2 使用实例"></a><font size=3>3.2 使用实例</font></h3><p>我们直接在刚才的hexo本地仓库试一下：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">git branch -vv</span></span><br><span class="line">* 4.2.1  e2cbad8e [origin/4.2.1] chore: incompatible with Node 14 (#4285)</span><br><span class="line">  master 981560f5 [origin/master] chore: bump c8 from 7.14.0 to 8.0.0 (#5227)</span><br></pre></td></tr></table></figure>

    </div>

    
    
    

    <footer class="post-footer">




    <div>
        
            <div style="text-align:center;color: #ccc;font-size:14px;">
            ----------本文结束
            <i class="fas fa-fan fa-spin" style="color: #FF1493; font-size: 1rem"></i>
            感谢您的阅读----------
            </div>
        
    </div>





  
  <div class="my_post_copyright"> 
    <p><span>文章标题:</span><a href="/post/fb1288db.html">LV01-Git-05-远程仓库-02-远程分支</a></p>
    <p><span>文章作者:</span><a href="/" title="欢迎访问 《苏木》 的学习笔记">苏木</a></p>
    <p><span>发布时间:</span>2023年07月02日 - 15:14</p>
    <p><span>最后更新:</span>2025年06月14日 - 00:25</p>
    <p><span>原始链接:</span><a href="/post/fb1288db.html" title="LV01-Git-05-远程仓库-02-远程分支">https://sumumm.github.io/post/fb1288db.html</a></p>
    <p><span>许可协议:</span><i class="fab fa-creative-commons"></i> <a rel="license" href= "https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">署名-非商业性使用-禁止演绎 4.0 国际</a> 转载请保留原文链接及作者。</p>  
  </div>
  


          <div class="post-tags">
              <a href="/tags/Git/" rel="tag"><i class="fa fa-tag"></i> Git</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/post/28cf0fcf.html" rel="prev" title="LV02-Git问题处理-01-端口连接被拒绝">
                  <i class="fa fa-angle-left"></i> LV02-Git问题处理-01-端口连接被拒绝
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/post/754a0881.html" rel="next" title="LV01-Git-05-远程仓库-01-GitHub简介">
                  LV01-Git-05-远程仓库-01-GitHub简介 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2017 – 
    <span itemprop="copyrightYear">2025</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">苏木</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
      <span>站点总字数：</span>
    <span title="站点总字数">3.7m</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span>站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">225:26</span>
  </span>
</div>




    <span id="sitetime"></span>
    <script defer language=javascript>
        function siteTime()
        {
            window.setTimeout("siteTime()", 1000);
            var seconds = 1000;
            var minutes = seconds * 60;
            var hours = minutes * 60;
            var days = hours * 24;
            var years = days * 365;
            var today = new Date();
            var todayYear = today.getFullYear();
            var todayMonth = today.getMonth()+1;
            var todayDate = today.getDate();
            var todayHour = today.getHours();
            var todayMinute = today.getMinutes();
            var todaySecond = today.getSeconds();
            /*==================================================
            Date.UTC() -- 返回date对象距世界标准时间(UTC)1970年1月1日午夜之间的毫秒数(时间戳)
            year        - 作为date对象的年份，为4位年份值
            month       - 0-11之间的整数，做为date对象的月份
            day         - 1-31之间的整数，做为date对象的天数
            hours       - 0(午夜24点)-23之间的整数，做为date对象的小时数
            minutes     - 0-59之间的整数，做为date对象的分钟数
            seconds     - 0-59之间的整数，做为date对象的秒数
            microseconds - 0-999之间的整数，做为date对象的毫秒数
            ==================================================*/
            var t1 = Date.UTC(2017, 
                              5, 
                              19, 
                              0, 
                              0, 
                              0); //北京时间
            var t2 = Date.UTC(todayYear,todayMonth,todayDate,todayHour,todayMinute,todaySecond);
            var diff = t2-t1;
            var diffYears = Math.floor(diff/years);
            var diffDays = Math.floor((diff/days)-diffYears*365);
            var diffHours = Math.floor((diff-(diffYears*365+diffDays)*days)/hours);
            var diffMinutes = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours)/minutes);
            var diffSeconds = Math.floor((diff-(diffYears*365+diffDays)*days-diffHours*hours-diffMinutes*minutes)/seconds);
            document.getElementById("sitetime").innerHTML="已在这里 "+diffYears+" 年 "+diffDays+" 天 "+diffHours+" 小时 "+diffMinutes+" 分钟 "+diffSeconds+" 秒";
        }
        siteTime();
    </script>



    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.28/fancybox/fancybox.umd.js" integrity="sha256-ytMJGN3toR+a84u7g7NuHm91VIR06Q41kMWDr2pq7Zo=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>




  <script src="/js/third-party/fancybox.js"></script>

  <script src="/js/third-party/pace.js"></script>


  




  

  <script class="next-config" data-name="enableMath" type="application/json">false</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


 
        <div id="click-show-text"
            data-mobile = false
            data-text = 富强,民主,文明,和谐,自由,平等,公正,法制,爱国,敬业,诚信,友善
            data-fontsize = 15px
            data-random= false>
        </div>
       

      
        <script async src=https://cdn.jsdelivr.net/npm/hexo-next-mouse-effect@latest/click/showText.js></script>
      

      
    




    <script async src="/js/fancybox_param.js"></script>





<!-- APlayer本体 -->



</body>
</html>
